//@ts-ignore
const erpService = require('../../services/erp-service');
const log = require('../../utils/log');
const util = require('../../utils/util');
const app = getApp();
let page_index = 1;
let page_index_xh = 1;
let page_index_sh = 1;
let page_index_ck = 1;
let nowDay="";
let nowDay2="";
let now_1 = "";
Page({
  data: {
    tab_list:[{
      "pagePath": "/pages/erp-index/erp-index",
      "text": "功能",
      "iconFont": "icon-tab-gongneng"
    }, {
      "pagePath": "/pages/erp-statistics/erp-statistics",
      "text": "统计",
      "iconFont": "icon-tab-shuju"
    }, {
      "pagePath": "/pages/erp-record/erp-record",
      "text": "单据",
      "iconFont": "icon-tab-danju"
    }],
    tabIndex:1,
    date_kc:nowDay2,//库存查询时间
    typeArr_kc:["所有状态","缺货状态","在途状态"],
    typeIndex_kc:0 as number|string,
    date_xh_start:"",//消耗查询开始时间
    date_xh_end:"",//消耗查询结束时间
    date_sh_start:"",//收货查询开始时间
    date_sh_end:"",//收货查询结束时间
    date_ck_start:"",//收货查询开始时间
    date_ck_end:"",//收货查询结束时间
    currentCategoryId_kc: 0,
    currentCategoryId_xh: 0,
    currentCategoryId_sh: 0,
    currentCategoryId_ck: 0,
    // all_materials_kc: [] as AllRawMaterialType[],
    // all_materials_xh: [] as AllRawMaterialType[],//所有消耗数据
    // all_materials_sh: [] as AllRawMaterialType[],//所有收货数据
    // all_materials_ck: [] as AllRawMaterialType[],//所有出库数据
    // all_materials_kc_all: [] as AllRawMaterialType[],
    // all_materials_xh_all: [] as AllRawMaterialType[],
    // all_materials_sh_all: [] as AllRawMaterialType[],
    // all_materials_ck_all: [] as AllRawMaterialType[],
    show_materials_kc:[],//显示出来的库存数据
    show_materials_xh:[],//显示出来的消耗数据
    show_materials_sh:[],//显示出来的收货数据
    show_materials_ck:[],//显示出来的出库数据
    headerHeight_kc: 0,
    bottomHeight_kc: 0,
    pageList_kc:[] as AllRawMaterialsPagType[],
    pageHeight_kc: [] as PageHeightType[],
    scrollH:0,
    headerHeight_xh: 0,
    bottomHeight_xh: 0,
    pageList_xh:[] as AllRawMaterialsPagType[],
    pageHeight_xh: [] as PageHeightType[],
    headerHeight_sh: 0,
    bottomHeight_sh: 0,
    pageList_sh:[] as AllRawMaterialsPagType[],
    pageHeight_sh: [] as PageHeightType[],
    headerHeight_ck: 0,
    bottomHeight_ck: 0,
    pageList_ck:[] as AllRawMaterialsPagType[],
    pageHeight_ck: [] as PageHeightType[],
    loadComplete_kc:false,
    loadComplete_xh:false,
    loadComplete_sh:false,
    loadComplete_ck:false,
    filter_dates: [{
      name: '今日',
    }, {
      name: '昨日',
    }, {
      name: '上周最后一日',
    }, {
      name: '上月最后一日',
    }, {
      name: '自定义某日',
    }],
    filterDatesIndex_kc:0,
    filter_date_name_kc:'',
    filter_date_word_kc:'今日',
    filter_date_choose_kc: 0,
    filter_dates_xh: [{
      name: '今日',
    }, {
      name: '昨日',
    }, {
      name: '本周',
    }, {
      name: '上周',
    }, {
      name: '本月',
    }, {
      name: '上月',
    }, {
      name: '自定义区间',
    }],
    filter_date_choose_xh: 0,
    filter_date_name_xh: '本月',
    filter_date_choose_sh: 0,
    filter_date_choose_ck: 0,
    showDateRange: false,
    frosted_glass: false,
    show_tab: true,
    loadTabsCon:{//四个模块分开加载
      kc:true,
      xh:true,
      sh:true,
      ck:true
    },
    keyWord_kc:"",
    kc_all_show: false,
    xh_all_show: false,
    sh_all_show: false,
    ck_all_show: false,
    // materialList_all: [],
    amtion:false,
    materialList_kc:[] as RawMaterialClassification[],
    materialList_kc_all:[] as RawMaterialClassification[],
    choose_repository:{} as any,
    screen_button:false,
    range_options:{
      ylfw:'hcyl',
      kcdw: true,
      cbdw:false
    } as any,
    range_options_xh:{
      ylfw:'hcyl',
      kcdw: true,
      cbdw:false
    }as any,
    screen_button_bule:false,
    classification_load:false,
  },
  data_snapshot:null as any,
  //保持四个模块的滑动状态
  scroll_top:{
    kc:{
      left:0,
      right:0
    },
    xh:{
      left:0,
      right:0
    },
    sh:{
      left:0,
      right:0
    },
    ck:{
      left:0,
      right:0
    }
  },
  all_materials_kc: [] as AllRawMaterialType[],
  all_materials_xh: [] as AllRawMaterialType[],//所有消耗数据
  all_materials_sh: [] as AllRawMaterialType[],//所有收货数据
  all_materials_ck: [] as AllRawMaterialType[],//所有出库数据
  all_materials_kc_all:[] as AllRawMaterialType[],
  all_materials_xh_all:[] as AllRawMaterialType[],
  all_materials_sh_all:[] as AllRawMaterialType[],
  all_materials_ck_all:[] as AllRawMaterialType[],
  //筛选重置初始值
  range_options:{
    ylfw:'hcyl',
    kcdw: true,
    cbdw:false
  } as any,
  range_options_kc:{},
  range_options_xh:{},
  range_options_sh:{},
  range_options_ck:{},
  last_range_options:{
    ylfw:'hcyl',
    kcdw: true,
    cbdw:false
  },
  materialList_all:[] as Array<any>,
  tabChange(e:any) { 
    wx.redirectTo({
       url: e.detail.item.pagePath
    })
  },
  tabHeadClick:function (e:any){
    const index = e.target.dataset.index
    let range_options = this.data.range_options
    let screen_button_bule = this.data.screen_button_bule
    //切换tab后筛选值变成之前已选的值
    if (index==1) {
      range_options = JSON.parse(JSON.stringify(this.range_options_kc))
      this.range_options = JSON.parse(JSON.stringify(this.range_options_kc))
      this.last_range_options = JSON.parse(JSON.stringify(range_options))
    }
    if (index==2) {
      range_options = JSON.parse(JSON.stringify(this.range_options_xh))
      this.range_options = JSON.parse(JSON.stringify(this.range_options_xh))
      this.last_range_options = JSON.parse(JSON.stringify(range_options))
    }
    if (index==3) {
      range_options = JSON.parse(JSON.stringify(this.range_options_sh))
      this.range_options = JSON.parse(JSON.stringify(this.range_options_sh))
      this.last_range_options = JSON.parse(JSON.stringify(range_options))
    }
    if (index==4) {
      range_options = JSON.parse(JSON.stringify(this.range_options_ck))
      this.range_options = JSON.parse(JSON.stringify(this.range_options_ck))
      this.last_range_options = JSON.parse(JSON.stringify(range_options))
    }
    if (range_options.ylfw=='hcyl'&&range_options.kcdw==true&&range_options.cbdw==false) {
      screen_button_bule = false
    }else{
      screen_button_bule = true
    }
    this.setData({
      tabIndex:index,
      showDateRange:false,
      screen_button:false,
      range_options,
      screen_button_bule
    },()=>{
      this.getKcList();
      this.getXhList();
      this.getShList();
      this.getCkList();
    })
  },
  pickerChange_kc: function(e:any) {
    this.setData({
      typeIndex_kc: e.detail.value
    },()=>{
      let {
        materialList_kc,
        typeIndex_kc,
        materialList_kc_all
      }=this.data;
      for(let i=0,j=materialList_kc.length;i<j;i++){
        if(typeIndex_kc==0){
          materialList_kc[i].isShow=true;
          materialList_kc_all[i].isShow=true;
        }else if(typeIndex_kc==2){
          if(materialList_kc[i].way_count){
            materialList_kc[i].isShow=true;
            materialList_kc_all[i].isShow=true;
          }else{
            materialList_kc[i].isShow=false;
            materialList_kc_all[i].isShow=false;
          }
        }else if(typeIndex_kc==1){
          if(materialList_kc[i].qhCount){
            materialList_kc[i].isShow=true;
            materialList_kc_all[i].isShow=true;
          }else{
            materialList_kc[i].isShow=false;
            materialList_kc_all[i].isShow=false;
          }
        }
      }
      for(let i=0,j=materialList_kc_all.length;i<j;i++){
        if(typeIndex_kc==0){
          materialList_kc_all[i].isShow=true;
        }else if(typeIndex_kc==2){
          if(materialList_kc_all[i].way_count){
            materialList_kc_all[i].isShow=true;
          }else{
            materialList_kc_all[i].isShow=false;
          }
        }else if(typeIndex_kc==1){
          if(materialList_kc_all[i].qhCount){
            materialList_kc_all[i].isShow=true;
          }else{
            materialList_kc_all[i].isShow=false;
          }
        }
      }
      page_index = 1;
      let current_array = this.makeCurrentArray(0,this.all_materials_kc);
      this.setData({
        materialList_kc,
        materialList_kc_all,
        currentCategoryId_kc: 0,
        left_top_kc: 0,
        right_top_kc: 0,
        pageList_kc:[{
          data:current_array.slice(0, 20),
          visible:true
        }],
        headerHeight_kc: 0,
        bottomHeight_kc: 0,
        pageHeight_kc: [{
          top: 0,
          height: 0,
          bottom: 0,
        }],
        loadComplete_kc:false
      },()=>{
        setTimeout(() => {
          this.initPageHeightKc(0);
        }, 100);
      });
      this.scroll_top.kc.left = 0;
      this.scroll_top.kc.right = 0;
    })
  },
  radioKc(){
    wx.showLoading({title:'',mask:true});
    let kc_all_show = !this.data.kc_all_show;
    let {
      all_materials_kc,
      // all_materials_kc_all
    }=this;
    let all_materials_kc_all = this.all_materials_kc_all
    page_index = 1;
    let current_array = this.makeCurrentArray(0,kc_all_show?all_materials_kc_all:all_materials_kc);
    this.setData({
      kc_all_show,
      currentCategoryId_kc: 0,
      left_top_kc: 0,
      right_top_kc: 0,
      pageList_kc:[{
        data:current_array.slice(0, 20),
        visible:true
      }],
      headerHeight_kc: 0,
      bottomHeight_kc: 0,
      pageHeight_kc: [{
        top: 0,
        height: 0,
        bottom: 0,
      }],
      loadComplete_kc:false
    },()=>{
      this.range_options = JSON.parse(JSON.stringify(this.data.range_options))
      this.range_options_kc = JSON.parse(JSON.stringify(this.data.range_options))
      setTimeout(()=>{
        wx.hideLoading();
      },300)
      setTimeout(() => {
        this.initPageHeightKc(0);
      }, 100);
    });
    this.scroll_top.kc.left = 0;
    this.scroll_top.kc.right = 0;
  },
  radioXh(){
    wx.showLoading({title:'',mask:true});
    let xh_all_show = this.data.xh_all_show;
    let range_options = this.data.range_options
    let range_options_obj = this.range_options
    if (range_options.ylfw!=range_options_obj.ylfw) {
      xh_all_show = !xh_all_show;
    }
    let {
      all_materials_xh,
      // all_materials_xh_all
    }=this;
    let all_materials_xh_all = this.all_materials_xh_all
    page_index_xh = 1;
    let current_array = this.makeCurrentArray(0,xh_all_show?all_materials_xh_all:all_materials_xh);
    this.setData({
      xh_all_show,
      currentCategoryId_xh: 0,
      left_top_xh:0,
      right_top_xh: 0,
      pageList_xh:[{
        data:current_array.slice(0, 20),
        visible:true
      }],
      headerHeight_xh: 0,
      bottomHeight_xh: 0,
      pageHeight_xh: [{
        top: 0,
        height: 0,
        bottom: 0,
      }],
      loadComplete_xh:false,
      range_options_xh:range_options
    },()=>{
      this.range_options = JSON.parse(JSON.stringify(this.data.range_options))
      this.range_options_xh = JSON.parse(JSON.stringify(this.data.range_options))
      setTimeout(()=>{
        wx.hideLoading();
      },300)
      setTimeout(() => {
        this.initPageHeightXh(0);
      }, 100);
    });
    this.scroll_top.xh.left = 0;
    this.scroll_top.xh.right = 0;
  },
  radioSh(){
    wx.showLoading({title:'',mask:true});
    let sh_all_show = !this.data.sh_all_show;
    let {
      all_materials_sh,
      // all_materials_sh_all
    }=this;
    let all_materials_sh_all = this.all_materials_sh_all
    page_index_sh = 1;
    let current_array = this.makeCurrentArray(0,sh_all_show?all_materials_sh_all:all_materials_sh);
    this.setData({
      sh_all_show,
      currentCategoryId_sh: 0,
      left_top_sh:0,
      right_top_sh: 0,
      pageList_sh:[{
        data:current_array.slice(0, 20),
        visible:true
      }],
      headerHeight_sh: 0,
      bottomHeight_sh: 0,
      pageHeight_sh: [{
        top: 0,
        height: 0,
        bottom: 0,
      }],
      loadComplete_sh:false
    },()=>{
      this.range_options = JSON.parse(JSON.stringify(this.data.range_options))
      this.range_options_sh = JSON.parse(JSON.stringify(this.data.range_options))
      setTimeout(()=>{
        wx.hideLoading();
      },300)
      setTimeout(() => {
        this.initPageHeightSh(0);
      }, 100);
    });
    this.scroll_top.sh.left = 0;
    this.scroll_top.sh.right = 0;
  },
  radioCk(){
    wx.showLoading({title:'',mask:true});
    let ck_all_show = !this.data.ck_all_show;
    let {
      all_materials_ck,
      // all_materials_ck_all
    }=this;
    let all_materials_ck_all = this.all_materials_ck_all
    page_index_ck = 1;
    let current_array = this.makeCurrentArray(0,ck_all_show?all_materials_ck_all:all_materials_ck);
    this.setData({
      ck_all_show,
      currentCategoryId_ck: 0,
      left_top_ck:0,
      right_top_ck: 0,
      pageList_ck:[{
        data:current_array.slice(0, 20),
        visible:true
      }],
      headerHeight_ck: 0,
      bottomHeight_ck: 0,
      pageHeight_ck: [{
        top: 0,
        height: 0,
        bottom: 0,
      }],
      loadComplete_ck:false
    },()=>{
      this.range_options = JSON.parse(JSON.stringify(this.data.range_options))
      this.range_options_ck = JSON.parse(JSON.stringify(this.data.range_options))
      setTimeout(()=>{
        wx.hideLoading();
      },300)
      setTimeout(() => {
        this.initPageHeightCk(0);
      }, 100);
    });
    this.scroll_top.ck.left = 0;
    this.scroll_top.ck.right = 0;
  },
  onLoad: function(option) {
    nowDay=util.formatDate(new Date());
    nowDay2=nowDay.replace(/-/g,"/");
    now_1 = util.formatDate(new Date(new Date().getTime() - 1 * 24 * 60 * 60 * 1000));
    let nowMonth=util.currentMonthLast();
    let noewMonthSta=nowMonth.startDate;
    let noewMonthEnd=nowDay;
    //@ts-ignore 
    let filter_date_name= util.getFilterDateName(noewMonthSta,noewMonthEnd);
    let filter_date_name_kc = this.getFilterDateNameKc(nowDay);
    //计算自定义标题的高度和top
    let stateHeight = 0;
    let navHeight = wx.getMenuButtonBoundingClientRect();
    this.range_options_kc = JSON.parse(JSON.stringify(this.data.range_options))
    this.range_options_xh = JSON.parse(JSON.stringify(this.data.range_options))
    this.range_options_sh = JSON.parse(JSON.stringify(this.data.range_options))
    this.range_options_ck = JSON.parse(JSON.stringify(this.data.range_options))
    wx.getSystemInfo({
       success(res){
       stateHeight = res.statusBarHeight
      }
    })
    this.setData({
      filter_date_name_kc,
      date_xh_start:nowDay,
      date_xh_end:nowDay,
      filter_date_name_xh:filter_date_name_kc,
      date_sh_start:nowDay,
      date_sh_end:nowDay,
      filter_date_name_sh:filter_date_name_kc,
      date_ck_start:nowDay,
      date_ck_end:nowDay,
      filter_date_name_ck:filter_date_name_kc,
      date_kc:nowDay2,
      filter_date_word_xh:'今日',
      filter_date_word_sh:'今日',
      filter_date_word_ck:'今日',
      navHeight: navHeight,
      stateHeight: stateHeight	
    })
    //  console.log("app.data.home_choose_repository",app.data.home_choose_repository)
    let auth_info = app.data.authInfo;
    if(!auth_info){//没有auth_info时页面停止数据加载
      return
    }
    //从外部进入时
    if(option.entrance=='functionApplication'){
      this.setData({
        show_tab:false
      })
      erpService.getAccountRepositories(true).then((success:any) => {
        if(success.length == 0){
          this.setData({
            frosted_glass:true
          })
          return
        }
        let repositories = success;
        let choose_repository = null;
        let _repository_id = repositories[0].id;
        repositories.forEach((item:any) => {
          item.show_name = item.name.replace(/（/g, '(');
          item.show_name = item.show_name.replace(/）/g, ')');
          item.checked = false;
          if (item.id == _repository_id) {
            item.checked = true;
            choose_repository = item;
          }
        })
        this.setData({
          storeList: repositories,
          choose_repository
        },()=>{
          //功能应用页面过来
          if(repositories.length > 1){
            this.chooseRepository()
          }
          this.findRepositoryData();
        })
      })
      //获取所有门店列表
      erpService.getAllRepository({
        // hq_id: auth_info.hqId,
        scene: 'LBS'
      }).then((success:any) => {
        if (!success) {
          log.info({
            host: util.ERP_API_HOST,
            url: `/metadata/merchant/all-repository`,
            params: {
              hq_id: auth_info.hqId,
            }
          });
          log.setFilterMsg('getAccountRepositories');
        }
        app.data.all_repositories = success.repository_list;
      }).catch(() =>{
        
      })
    }else{
      erpService.getAccountRepositories().then((success:any) => {
        let repositories = success;
        let choose_repository = null;
        let _repository_id = Object.keys(app.data.home_choose_repository).length ? app.data.home_choose_repository.id : repositories[0].id;
        repositories.forEach((item:any) => {
          item.show_name = item.name.replace(/（/g, '(');
          item.show_name = item.show_name.replace(/）/g, ')');
          item.checked = false;
          if (item.id == _repository_id) {
            item.checked = true;
            choose_repository = item;
          }
        })
        this.setData({
          storeList: repositories,
          choose_repository
        },()=>{
          this.findRepositoryData();
        })
      })
    }
  },
  goDetail(e:any) {
    const item = e.currentTarget.dataset.item;
    const {
      choose_repository,
      tabIndex,
      date_kc,
      date_xh_start,
      date_xh_end,
      date_sh_start,
      date_sh_end,
      date_ck_start,
      date_ck_end,
    } = this.data;
    console.log(item);
    let startTime="",endTime="";
    let url = '/pages/erp-repository-item/erp-repository-item?';
    if(tabIndex==1){
      startTime=date_kc.replace(/\//g,"-");
      endTime=date_kc.replace(/\//g,"-");
    }else if(tabIndex==2){
      startTime=date_xh_start;
      endTime=date_xh_end;
      url= '/subpackages/erp-subpackges/erp-consumption-details/erp-consumption-details?';
    }else if(tabIndex==3){
      startTime=date_sh_start;
      endTime=date_sh_end;
      url = '/pages/erp-checkloss-report/erp-checkloss-report?from_page=import';
    }else if(tabIndex==4){
      startTime=date_ck_start;
      endTime=date_ck_end;
      url = '/pages/erp-checkloss-report/erp-checkloss-report?from_page=outbound';
    }
    let url_name = encodeURIComponent(item.name);
    let url_code = encodeURIComponent(item.code)
    wx.navigateTo({
      url: url + '&code=' + url_code + '&name=' + url_name + '&deposit_unit=' + item.deposit_unit + '&material_id=' + item.id + '&repository_id=' + choose_repository.id+'&startTime='+startTime+'&endTime='+endTime + '&spec='+item.spec
    });
  },

  findRepositoryData() {
    wx.showLoading({title:'',mask:true});
    const {
      // hqId,
      // accountId
    } = app.data.authInfo;
    const {
      choose_repository,
    } = this.data;
    
    erpService.findAllRawMaterial( choose_repository.id).then((res:any) => {
      let materialList_all = res.material_category_list.filter((item:any) => item.materials.length);
      this.materialList_all = materialList_all
      this.setData({
        // materialList_all:materialList_all
      },()=>{
        this.getKcList();
        this.getXhList();
        this.getShList();
        this.getCkList();
      })
    }).catch(()=>{
      wx.hideLoading();
    });
  },
  repositoryDataMap:{} as any,
  getKcList(callback?:any){
    let {
      choose_repository,
      date_kc,
      loadTabsCon,
      tabIndex,
      kc_all_show,
      amtion
    } = this.data;
    if(!loadTabsCon.kc&& !amtion || tabIndex!=1&& !amtion){
      this.setData({
        left_top_kc:this.scroll_top.kc.left,
        right_top_kc:this.scroll_top.kc.right,
      })
      return
    }
    page_index = 1;
    const {
      hqId,
      accountId
    } = app.data.authInfo;
    let date_kc2=date_kc.replace(/\//g,"");
    wx.showLoading({title:'',mask:true});
    erpService.getDayInventoryList({hq_id:hqId, account_id:accountId,date:date_kc2,repository_id:choose_repository.id},60000).then((res1:any) => {
       let {
        typeIndex_kc
       }=this.data;
       let materialList_all =JSON.parse(JSON.stringify(this.materialList_all)); 
       let repositoryDataMap:any = {};
      res1.report_rows.forEach((item:any) => {
        repositoryDataMap[item.material_id] = item;
      });
     
      let materialList_kc=[],all_materials_kc = [];
      let materialList_kc_all= [],all_materials_kc_all = [];
       // item.count < 0 //缺货
        // item.way_count != 0 //在途
      for(let i of materialList_all){
        let group=0,qhCount=0,way_count=0,isShow=false;
        for(let item of i.materials){
          let kcitem=repositoryDataMap[item.id];
          if(kcitem){
            if(item.id==kcitem.material_id){
              group+=1;
              if(kcitem.count <= 0){
                qhCount+=1;
              }
              if(kcitem.way_count != 0){
                way_count+=1;
              }
              item.count=kcitem.count;
              item.waste=Math.abs(kcitem.count);
              item.way_count=kcitem.way_count;
              all_materials_kc.push(item);
              all_materials_kc_all.push(item);
            }
          }else{
            item.count = '';
            item.way_count = '';
            all_materials_kc_all.push(item);
          }
        }
        if(typeIndex_kc==0){
          isShow=true;
        }else if(typeIndex_kc==2 && way_count){
          isShow=true;
        }else if(typeIndex_kc==1 && qhCount){
          isShow=true;
        }
        if(group){
          materialList_kc.push({id:i.id,name:i.name,qhCount,way_count,isShow:isShow});
        }
        materialList_kc_all.push({id:i.id,name:i.name,qhCount,way_count,isShow:isShow});
      }
      let current_array = this.makeCurrentArray(0,kc_all_show?all_materials_kc_all:all_materials_kc);
      loadTabsCon.kc=false;
      this.scroll_top.kc.left = 0;
      this.scroll_top.kc.right = 0;
      this.all_materials_kc_all = all_materials_kc_all;
      this.repositoryDataMap = repositoryDataMap;
      this.all_materials_kc = all_materials_kc
      this.setData({
        // repositoryDataMap,
        materialList_kc,
        // all_materials_kc,
        materialList_kc_all,
        // all_materials_kc_all,
        pageList_kc: [{
          data: current_array.slice(0, 20),
          visible: true,
        }],
        currentCategoryId_kc: 0,
        left_top_kc: 0,
        right_top_kc: 0,
        headerHeight_kc: 0,
        bottomHeight_kc: 0,
        pageHeight_kc: [{
          top: 0,
          height: 0,
          bottom: 0,
        }],
        loadComplete_kc:false,
        loadTabsCon,
        amtion:false
      }, () => {
        setTimeout(() => {
          this.initPageHeightKc(0);
        }, 100);
        wx.hideLoading();
      });
      callback&&callback(false)
    }).catch(()=>{
      this.setData({
        amtion:false
      })
      callback&&callback(true)
    });
  },
  //查询消耗列表
  getXhList(){
    let {
      date_xh_start,
      date_xh_end,
      choose_repository,
      loadTabsCon,
      tabIndex,
      xh_all_show,
      amtion
    }=this.data;
    if(!loadTabsCon.xh&&!amtion  || tabIndex!=2&&!amtion){
      this.setData({
        left_top_xh:this.scroll_top.xh.left,
        right_top_xh:this.scroll_top.xh.right,
      })
      return
    }
    page_index_xh = 1;
    const {
    } = app.data.authInfo;
    date_xh_start=date_xh_start.replace(/-/g,"")+"000000";
    date_xh_end=date_xh_end.replace(/-/g,"")+"235959";
    wx.showLoading({title:'',mask:true});
    // let params={
    //   hq_id:hqId,
    //   repository_id:choose_repository.id,
    //   account_id:accountId,
    //   start_datetime:date_xh_start,
    //   end_datetime:date_xh_end
    // };
    let params={
      // hq_id:hqId,
      repository_id:choose_repository.id,
      // account:{id:accountId,name:fullname},
      start_datetime:date_xh_start,
      end_datetime:date_xh_end
    };
    // choose_repository.id, choose_repository.branch_id
    erpService.getProductSaleList(params,60000).then((result:any) => {
      console.log("消耗",result)
      let materialList_all =JSON.parse(JSON.stringify(this.materialList_all)); 
      let repositoryDataMap:any = {};
      // result.report_rows.forEach((item:any) => {
      result.list.forEach((item:any) => {
        repositoryDataMap[item.material_id] = item;
      });

      let materialList_xh=[],all_materials_xh = [];
      let materialList_xh_all=[],all_materials_xh_all = [];
      for(let i of materialList_all){
        let group=0;
        for(let item of i.materials){
          let kcitem=repositoryDataMap[item.id];
          if(kcitem){
            if(item.id==kcitem.material_id){
              group+=1;
              item.real_count=kcitem.real_count;
              item.cost_amount=kcitem.cost_amount;
              item.usage_unit = kcitem.usage_unit;
              item.deposit_unit = kcitem.deposit_unit;
              // item.material_count=kcitem.material_count;
              all_materials_xh.push(item);
              all_materials_xh_all.push(item);
            }
          }else{
            // item.material_count = '';
            item.cost_amount = '';
            item.real_count = '';
            all_materials_xh_all.push(item);
          }
        }
        if(group){
          materialList_xh.push({id:i.id,name:i.name});
        }
        materialList_xh_all.push({id:i.id,name:i.name});
      }
      let current_array = this.makeCurrentArray(0,xh_all_show?all_materials_xh_all:all_materials_xh);
      loadTabsCon.xh=false;
      this.scroll_top.xh.left = 0;
      this.scroll_top.xh.right = 0;
      this.all_materials_xh_all = all_materials_xh_all
      this.all_materials_xh = all_materials_xh
      this.setData({
        materialList_xh,
        // all_materials_xh,
        materialList_xh_all,
        // all_materials_xh_all,
        currentCategoryId_xh:0,
        left_top_xh: 0,
        right_top_xh: 0,
        pageList_xh: [{
          data: current_array.slice(0, 20),
          visible: true,
        }],
        headerHeight_xh: 0,
        bottomHeight_xh: 0,
        pageHeight_xh: [{
          top: 0,
          height: 0,
          bottom: 0,
        }],
        loadComplete_xh:false,
        loadTabsCon,
        amtion:false
      },()=>{
        wx.hideLoading();
        setTimeout(() => {
          this.initPageHeightXh(0);
        }, 100);
      })
       
    }).catch(()=>{
      this.setData({
        amtion:false
      })
      wx.hideLoading();
    });
     
  },
  //查询收货列表
  getShList(){
    let {
      date_sh_start,
      date_sh_end,
      choose_repository,
      loadTabsCon,
      tabIndex,
      sh_all_show,
      amtion
    }=this.data;
    if(!loadTabsCon.sh&&!amtion || tabIndex!=3&&!amtion){
      this.setData({
        left_top_sh:this.scroll_top.sh.left,
        right_top_sh:this.scroll_top.sh.right,
      })
      return
    }
    page_index_sh = 1;
    const {
      // hqId,
      // accountId
    } = app.data.authInfo;
    date_sh_start=date_sh_start.replace(/-/g,"")+"000000";
    date_sh_end=date_sh_end.replace(/-/g,"")+"235959";
    wx.showLoading({title:'',mask:true});
    let params={
      // hq_id:hqId,
      repository_id:choose_repository.id,
      // account_id:accountId,
      start_datetime:date_sh_start,
      end_datetime:date_sh_end
    };
    // choose_repository.id, choose_repository.branch_id
    erpService.getWarehousingFlowSumList(params).then((result:any) => {
     console.log("入库",result)
      let materialList_all =JSON.parse(JSON.stringify(this.materialList_all)); 
      let repositoryDataMap:any = {};
      result.report_rows.forEach((item:any) => {
        repositoryDataMap[item.material_id] = item;
      });

      let materialList_sh=[],all_materials_sh = [];
      let materialList_sh_all=[],all_materials_sh_all = [];
      for(let i of materialList_all){
        let group=0;
        for(let item of i.materials){
          let kcitem=repositoryDataMap[item.id];
          if(kcitem){
            if(item.id==kcitem.material_id){
              group+=1;
              item.material_count=kcitem.material_count;
              all_materials_sh.push(item);
              all_materials_sh_all.push(item);
            }
          }else{
            item.material_count='';
            all_materials_sh_all.push(item);
          }

        }
        if(group){
          materialList_sh.push({id:i.id,name:i.name});
        }
        materialList_sh_all.push({id:i.id,name:i.name});
      }
      loadTabsCon.sh=false;
      let current_array = this.makeCurrentArray(0,sh_all_show?all_materials_sh_all:all_materials_sh);
      this.scroll_top.sh.left = 0;
      this.scroll_top.sh.right = 0;
      this.all_materials_sh_all = all_materials_sh_all;
      this.all_materials_sh = all_materials_sh;
      this.setData({
        materialList_sh,
        // all_materials_sh,
        materialList_sh_all,
        // all_materials_sh_all,
        currentCategoryId_sh:0,
        left_top_sh: 0,
        right_top_sh:0,
        pageList_sh: [{
          data: current_array.slice(0, 20),
          visible: true,
        }],
        headerHeight_sh: 0,
        bottomHeight_sh: 0,
        pageHeight_sh: [{
          top: 0,
          height: 0,
          bottom: 0,
        }],
        loadComplete_sh:false,
        loadTabsCon,
        amtion:false
      }, () => {
        wx.hideLoading();
        setTimeout(() => {
          this.initPageHeightSh(0);
        }, 100);
      })
       
    }).catch(()=>{
      this.setData({
        amtion:false
      })
      wx.hideLoading();
    });
     
  },
  //查询出库列表
  getCkList(){
    let {
      date_ck_start,
      date_ck_end,
      choose_repository,
      loadTabsCon,
      tabIndex,
      ck_all_show,
      amtion
    }=this.data;
    if(!loadTabsCon.ck&&!amtion || tabIndex!=4&&!amtion){
      this.setData({
        left_top_ck:this.scroll_top.ck.left,
        right_top_ck:this.scroll_top.ck.right,
      })
      return
    }
    page_index_ck = 1;
    const {
      // hqId,
      // accountId
    } = app.data.authInfo;
    date_ck_start=date_ck_start.replace(/-/g,"")+"000000";
    date_ck_end=date_ck_end.replace(/-/g,"")+"235959";
    wx.showLoading({title:'',mask:true});
    let params={
      // hq_id:hqId,
      repository_id:choose_repository.id,
      // account_id:accountId,
      start_datetime:date_ck_start,
      end_datetime:date_ck_end
    };
    // choose_repository.id, choose_repository.branch_id
    erpService.getDeliveryFlowSumList(params).then((result:any) => {
      console.log("出库",result)
      let materialList_all =JSON.parse(JSON.stringify(this.materialList_all));
      let repositoryDataMap:any = {};
      result.report_rows.forEach((item:any) => {
        repositoryDataMap[item.material_id] = item;
      });

      let materialList_ck=[],all_materials_ck = [];
      let materialList_ck_all=[],all_materials_ck_all = [];
      for(let i of materialList_all){
        let group=0;
        for(let item of i.materials){
          let kcitem=repositoryDataMap[item.id];
          if(kcitem){
            if(item.id==kcitem.material_id){
              group+=1;
              item.material_count=kcitem.material_count;
              item.item_id = kcitem.id;//货品id
              all_materials_ck.push(item);
              all_materials_ck_all.push(item);
            }
          }else{
            item.material_count = '';
            all_materials_ck_all.push(item);
          }
        }
        if(group){
          materialList_ck.push({id:i.id,name:i.name});
        }
        materialList_ck_all.push({id:i.id,name:i.name});
      }
      let current_array = this.makeCurrentArray(0,ck_all_show?all_materials_ck_all:all_materials_ck); 
      loadTabsCon.ck=false;
      this.scroll_top.ck.left = 0;
      this.scroll_top.ck.right = 0;
      this.all_materials_ck_all = all_materials_ck_all;
      this.all_materials_ck = all_materials_ck;
      this.setData({
        materialList_ck,
        // all_materials_ck,
        materialList_ck_all,
        // all_materials_ck_all,
        currentCategoryId_ck:0,
        left_top_ck: 0,
        right_top_ck: 0,
        pageList_ck: [{
          data: current_array.slice(0, 20),
          visible: true,
        }],
        headerHeight_ck: 0,
        bottomHeight_ck: 0,
        pageHeight_ck: [{
          top: 0,
          height: 0,
          bottom: 0,
        }],
        loadComplete_ck:false,
        loadTabsCon,
        amtion:false
      }, () => {
        wx.hideLoading();
        setTimeout(() => {
          this.initPageHeightCk(0);
        }, 100);
      })
       
    }).catch(()=>{
      this.setData({
        amtion:false
      })
      wx.hideLoading();
    });
     
  },
   /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    const query = wx.createSelectorQuery();
    query.select('.content-wrap').boundingClientRect().exec((res) => {
      this.setData({
        scrollH: res[0]?res[0].height:600
      })
    })
  },
  onShow: function() {},
  onHide: function() {},
  chooseRepository() {
    this.setData({
      show_repository: true,
    })
  },
  checkStore(e:any) {
    this.setData({
      show_repository: false
    })
    let choosed = {} as ChooseRepositoryType;
    e.detail.dataList.forEach((item:any) => {
      if (item.checked) {
        choosed = item;
      }
    })
    if (!choosed || this.data.choose_repository.id == choosed.id) {
      return
    }
    let {loadTabsCon}=this.data;
    loadTabsCon={
      kc:true,
      xh:true,
      sh:true,
      ck:true
    }
    
    this.setData({
      choose_repository: choosed,
      storeList: e.detail.dataList,
      loadTabsCon
    }, () => {
      this.findRepositoryData();
    })
  },
  // 初始化首页高度
  initPageHeightKc(index:number) {
    const query = wx.createSelectorQuery();
    query.select(`#listPageId${index}`).boundingClientRect().exec((res) => {
      let pageHeight = this.data.pageHeight_kc;
      if (((index > 0 && pageHeight[index - 1]) || index == 0) && res[0]) {
        pageHeight[index] = {
          top: index > 0 ? pageHeight[index - 1].bottom + 1 : 0, // 顶部在scroll里的高度
          height: res[0].height,
          bottom: index > 0 ?
            util.floatObj.add(pageHeight[index - 1].bottom + 1, res[0].height) : res[0].height, // 底部在scroll里的高度
        }
      } else {
        pageHeight[index] = {
          top: 0,
          height: 0,
          bottom: 0
        }
      }
      this.setData({
        pageHeight_kc: pageHeight
      })
    })
  },
  bindscrollLeftKc(e:any){
    this.scroll_top.kc.left =  e.detail.scrollTop;
  },
  bindscrollLeftXh(e:any){
    this.scroll_top.xh.left =  e.detail.scrollTop;
  },
  bindscrollLeftSh(e:any){
    this.scroll_top.sh.left =  e.detail.scrollTop;
  },
  bindscrollLeftCk(e:any){
    this.scroll_top.ck.left =  e.detail.scrollTop;
  },
  // 滚动
  bindscrollKc(e:any) {
    // 实现虚拟列表
    let pageList = this.data.pageList_kc, headerHeight = this.data.headerHeight_kc, bottomHeight = this.data.bottomHeight_kc;
    let {scrollH} = this.data;
    //保存当前scroll的滚动位置
    this.scroll_top.kc.right = e.detail.scrollTop;
    this.data.pageHeight_kc.forEach((item, index) => {
      // 手指上滑
      if (e.detail.deltaY < 0 && item.bottom < util.floatObj.subtract(e.detail.scrollTop, 10) && pageList[index] && pageList[index].visible && pageList[index + 2]) {
        // 隐藏头部
        pageList[index].visible = false;
        headerHeight += item.height;
        // 显示底部
        if (!pageList[index + 2].visible) {
          pageList[index + 2].visible = true;
          bottomHeight -= this.data.pageHeight_kc[index + 2].height
        }
        this.setData({
          pageList_kc: pageList,
          headerHeight_kc: headerHeight,
          bottomHeight_kc: bottomHeight
        })
      }

      // 手指下滑
      if (e.detail.deltaY > 0 && item.top > util.floatObj.add(e.detail.scrollTop, util.floatObj.add(scrollH, 10)) && pageList[index] && pageList[index].visible && pageList[index - 2]) {

        // 隐藏头部
        pageList[index].visible = false;
        bottomHeight += item.height;

        if (!pageList[index - 2].visible) {
          // 显示底部
          pageList[index - 2].visible = true;
          headerHeight -= this.data.pageHeight_kc[index - 2].height
        }

        this.setData({
          pageList_kc: pageList,
          headerHeight_kc: headerHeight,
          bottomHeight_kc: bottomHeight
        })
      }
    })
  },
  //每页20条加载数据
  moreMaterialsKc() {
    if (this.data.loadComplete_kc) return;
    page_index++;
    let start = (page_index - 1) * 20;
    let end = page_index * 20;
    let listData = this.makeCurrentArray(this.data.currentCategoryId_kc,this.data.kc_all_show?this.all_materials_kc_all:this.all_materials_kc).slice(start, end);
    let tampParam:any = {};
    if (listData.length < 20) {
      tampParam = {
        loadComplete_kc: true
      }
    }
   
    let {pageList_kc,pageHeight_kc} = this.data;
    if (listData.length > 0) {

      pageList_kc.push({
        data: listData,//数据
        visible: true,// 当前是否显示
      })

      pageHeight_kc.push({
        top: 0, // 顶部在scroll里的高度
        height: 0, // 高度
        bottom: 0, // 底部在scroll里的高度
      })
      tampParam.pageList_kc = pageList_kc
      tampParam.pageHeight_kc = pageHeight_kc
    }
    this.setData(tampParam)
    if (listData.length > 0) {
      setTimeout(() => {
        this.initPageHeightKc(page_index - 1)
      }, 100);
    }
  },

  // 初始化消耗高度
  initPageHeightXh(index:number) {
    const query = wx.createSelectorQuery();
    query.select(`#listPageIdXh${index}`).boundingClientRect().exec((res) => {
      let pageHeight = this.data.pageHeight_xh;
      if (((index > 0 && pageHeight[index - 1]) || index == 0) && res[0]) {
        pageHeight[index] = {
          top: index > 0 ? pageHeight[index - 1].bottom + 1 : 0, // 顶部在scroll里的高度
          height: res[0].height,
          bottom: index > 0 ?
            util.floatObj.add(pageHeight[index - 1].bottom + 1, res[0].height) : res[0].height, // 底部在scroll里的高度
        }
      } else {
        pageHeight[index] = {
          top: 0,
          height: 0,
          bottom: 0
        }
      }
      this.setData({
        pageHeight_xh: pageHeight
      })
    })
  },
  // 消耗滚动
  bindscrollXh(e:any) {
    // 实现虚拟列表
    let pageList = this.data.pageList_xh, headerHeight = this.data.headerHeight_xh, bottomHeight = this.data.bottomHeight_xh;
    let {scrollH} = this.data;
    //保存当前scroll的滚动位置
    this.scroll_top.xh.right = e.detail.scrollTop;
    this.data.pageHeight_xh.forEach((item, index) => {
      // 手指上滑
      if (e.detail.deltaY < 0 && item.bottom < util.floatObj.subtract(e.detail.scrollTop, 10) && pageList[index] && pageList[index].visible && pageList[index + 2]) {
        // 隐藏头部
        pageList[index].visible = false;
        headerHeight += item.height;
        // 显示底部
        if (!pageList[index + 2].visible) {
          pageList[index + 2].visible = true;
          bottomHeight -= this.data.pageHeight_xh[index + 2].height
        }
        this.setData({
          pageList_xh: pageList,
          headerHeight_xh: headerHeight,
          bottomHeight_xh: bottomHeight
        })
      }

      // 手指下滑
      if (e.detail.deltaY > 0 && item.top > util.floatObj.add(e.detail.scrollTop, util.floatObj.add(scrollH, 10)) && pageList[index] && pageList[index].visible && pageList[index - 2]) {

        // 隐藏头部
        pageList[index].visible = false;
        bottomHeight += item.height;

        if (!pageList[index - 2].visible) {
          // 显示底部
          pageList[index - 2].visible = true;
          headerHeight -= this.data.pageHeight_xh[index - 2].height
        }

        this.setData({
          pageList_xh: pageList,
          headerHeight_xh: headerHeight,
          bottomHeight_xh: bottomHeight
        })
      }
    })
  },
  //每页20条加载数据 消耗
  moreMaterialsXh() {
    if (this.data.loadComplete_xh) return;
    page_index_xh++;
    let start = (page_index_xh - 1) * 20;
    let end = page_index_xh * 20;
    let listData = this.makeCurrentArray(this.data.currentCategoryId_xh,this.data.xh_all_show?this.all_materials_xh_all:this.all_materials_xh).slice(start, end);
    let tampParam:any = {};
    if (listData.length < 20) {
      tampParam = {
        loadComplete_xh: true
      }
    }
    
    let {pageList_xh,pageHeight_xh} = this.data;
    if (listData.length > 0) {

      pageList_xh.push({
        data: listData,//数据
        visible: true,// 当前是否显示
      })

      pageHeight_xh.push({
        top: 0, // 顶部在scroll里的高度
        height: 0, // 高度
        bottom: 0, // 底部在scroll里的高度
      })
      tampParam.pageList_xh = pageList_xh
      tampParam.pageHeight_xh = pageHeight_xh
    }
    this.setData(tampParam)
    if (listData.length > 0) {
      setTimeout(() => {
        this.initPageHeightXh(page_index_xh - 1)
      }, 100);
    }
  },


  // 初始化收货高度
  initPageHeightSh(index:any) {
    const query = wx.createSelectorQuery();
    query.select(`#listPageIdSh${index}`).boundingClientRect().exec((res) => {
      let pageHeight = this.data.pageHeight_sh;
      if (((index > 0 && pageHeight[index - 1]) || index == 0) && res[0]) {
        pageHeight[index] = {
          top: index > 0 ? pageHeight[index - 1].bottom + 1 : 0, // 顶部在scroll里的高度
          height: res[0].height,
          bottom: index > 0 ?
            util.floatObj.add(pageHeight[index - 1].bottom + 1, res[0].height) : res[0].height, // 底部在scroll里的高度
        }
      } else {
        pageHeight[index] = {
          top: 0,
          height: 0,
          bottom: 0
        }
      }
      this.setData({
        pageHeight_sh: pageHeight
      })
    })
  },
  // 消耗滚动
  bindscrollSh(e:any) {
    // 实现虚拟列表
    let pageList = this.data.pageList_sh, headerHeight = this.data.headerHeight_sh, bottomHeight = this.data.bottomHeight_sh;
    let {scrollH} = this.data;
    //保存当前scroll的滚动位置
    this.scroll_top.sh.right = e.detail.scrollTop;
    this.data.pageHeight_sh.forEach((item, index) => {
      // 手指上滑
      if (e.detail.deltaY < 0 && item.bottom < util.floatObj.subtract(e.detail.scrollTop, 10) && pageList[index] && pageList[index].visible && pageList[index + 2]) {
        // 隐藏头部
        pageList[index].visible = false;
        headerHeight += item.height;
        // 显示底部
        if (!pageList[index + 2].visible) {
          pageList[index + 2].visible = true;
          bottomHeight -= this.data.pageHeight_sh[index + 2].height
        }
        this.setData({
          pageList_sh: pageList,
          headerHeight_sh: headerHeight,
          bottomHeight_sh: bottomHeight
        })
      }

      // 手指下滑
      if (e.detail.deltaY > 0 && item.top > util.floatObj.add(e.detail.scrollTop, util.floatObj.add(scrollH, 10)) && pageList[index] && pageList[index].visible && pageList[index - 2]) {

        // 隐藏头部
        pageList[index].visible = false;
        bottomHeight += item.height;

        if (!pageList[index - 2].visible) {
          // 显示底部
          pageList[index - 2].visible = true;
          headerHeight -= this.data.pageHeight_sh[index - 2].height
        }

        this.setData({
          pageList_sh: pageList,
          headerHeight_sh: headerHeight,
          bottomHeight_sh: bottomHeight
        })
      }
    })
  },
  //每页20条加载数据 消耗
  moreMaterialsSh() {
    if (this.data.loadComplete_sh) return;
    page_index_sh++;
    let start = (page_index_sh - 1) * 20;
    let end = page_index_sh * 20;
    let listData = this.makeCurrentArray(this.data.currentCategoryId_sh,this.data.sh_all_show?this.all_materials_sh_all:this.all_materials_sh).slice(start, end);
    let tampParam:any = {};
    if (listData.length < 20) {
      tampParam = {
        loadComplete_sh: true
      }
    }
    
    let {pageList_sh,pageHeight_sh} = this.data;
    if (listData.length > 0) {

      pageList_sh.push({
        data: listData,//数据
        visible: true,// 当前是否显示
      })

      pageHeight_sh.push({
        top: 0, // 顶部在scroll里的高度
        height: 0, // 高度
        bottom: 0, // 底部在scroll里的高度
      })
      tampParam.pageList_sh = pageList_sh
      tampParam.pageHeight_sh = pageHeight_sh
    }
    this.setData(tampParam)
    if (listData.length > 0) {
      setTimeout(() => {
        this.initPageHeightSh(page_index_sh - 1)
      }, 100);
    }
  },

  // 初始化出库高度
  initPageHeightCk(index:number) {
    const query = wx.createSelectorQuery();
    query.select(`#listPageIdCk${index}`).boundingClientRect().exec((res) => {
      let pageHeight = this.data.pageHeight_ck;
      if (((index > 0 && pageHeight[index - 1]) || index == 0) && res[0]) {
        pageHeight[index] = {
          top: index > 0 ? pageHeight[index - 1].bottom + 1 : 0, // 顶部在scroll里的高度
          height: res[0].height,
          bottom: index > 0 ?
            util.floatObj.add(pageHeight[index - 1].bottom + 1, res[0].height) : res[0].height, // 底部在scroll里的高度
        }
      } else {
        pageHeight[index] = {
          top: 0,
          height: 0,
          bottom: 0
        }
      }
      this.setData({
        pageHeight_ck: pageHeight
      })
    })
  },
  // 出库滚动
  bindscrollCk(e:any) {
    // 实现虚拟列表
    let pageList = this.data.pageList_ck, headerHeight = this.data.headerHeight_ck, bottomHeight = this.data.bottomHeight_ck;
    let {scrollH} = this.data;
    //保存当前scroll的滚动位置
    this.scroll_top.ck.right = e.detail.scrollTop;
    this.data.pageHeight_ck.forEach((item, index) => {
      // 手指上滑
      if (e.detail.deltaY < 0 && item.bottom < util.floatObj.subtract(e.detail.scrollTop, 10) && pageList[index] && pageList[index].visible && pageList[index + 2]) {
        // 隐藏头部
        pageList[index].visible = false;
        headerHeight += item.height;
        // 显示底部
        if (!pageList[index + 2].visible) {
          pageList[index + 2].visible = true;
          bottomHeight -= this.data.pageHeight_ck[index + 2].height
        }
        this.setData({
          pageList_ck: pageList,
          headerHeight_ck: headerHeight,
          bottomHeight_ck: bottomHeight
        })
      }

      // 手指下滑
      if (e.detail.deltaY > 0 && item.top > util.floatObj.add(e.detail.scrollTop, util.floatObj.add(scrollH, 10)) && pageList[index] && pageList[index].visible && pageList[index - 2]) {

        // 隐藏头部
        pageList[index].visible = false;
        bottomHeight += item.height;

        if (!pageList[index - 2].visible) {
          // 显示底部
          pageList[index - 2].visible = true;
          headerHeight -= this.data.pageHeight_ck[index - 2].height
        }

        this.setData({
          pageList_ck: pageList,
          headerHeight_ck: headerHeight,
          bottomHeight_ck: bottomHeight
        })
      }
    })
  },
  //每页20条加载数据 出库
  moreMaterialsCk() {
    if (this.data.loadComplete_ck) return;
    page_index_ck++;
    let start = (page_index_ck - 1) * 20;
    let end = page_index_ck * 20;
    let listData = this.makeCurrentArray(this.data.currentCategoryId_ck,this.data.ck_all_show?this.all_materials_ck_all:this.all_materials_ck).slice(start, end);
    let tampParam:any = {};
    if (listData.length < 20) {
      tampParam = {
        loadComplete_ck: true
      }
    }
    
    let {pageList_ck,pageHeight_ck} = this.data;
    if (listData.length > 0) {

      pageList_ck.push({
        data: listData,//数据
        visible: true,// 当前是否显示
      })

      pageHeight_ck.push({
        top: 0, // 顶部在scroll里的高度
        height: 0, // 高度
        bottom: 0, // 底部在scroll里的高度
      })
      tampParam.pageList_ck = pageList_ck
      tampParam.pageHeight_ck = pageHeight_ck
    }
    this.setData(tampParam)
    if (listData.length > 0) {
      setTimeout(() => {
        this.initPageHeightCk(page_index_ck - 1)
      }, 10);
    }
  },
  //返回符合当前条件的全部数据
  makeCurrentArray: function(id:number|string,allArr:Array<any>) {
    const {
      typeIndex_kc,
      tabIndex
    } = this.data;
    let current_array = [];
    if(!allArr){
      return []
    }
    if(tabIndex==1){
      current_array = allArr.filter(item => {
        // item.count <= 0 //缺货
        // item.way_count != 0 //在途
        if(typeIndex_kc == '0' && ( id == "0" || item.category_id == id) ){
          return item
        }else if(typeIndex_kc== 2 && item.way_count != 0 && ( id == "0" || item.category_id == id)){
          return item
        }else if (typeIndex_kc == 1 && item.count <= 0 && ( id == "0" || item.category_id == id)) {
          return item
        }
      })
    }else if(tabIndex==2||tabIndex==3||tabIndex==4){
      current_array = allArr.filter(item => {
        if(id == "0" || item.category_id == id){
          return item
        }
      })
    }
    
    return current_array
  },
  changeCategory: function(e:any) {
    let {tabIndex}=this.data;
    const id = e.currentTarget.dataset.id;
    if(tabIndex==1){
      if(id==this.data.currentCategoryId_kc){
        return
      }
      page_index = 1;
      this.setData({
        currentCategoryId_kc: id,
        right_top_kc: 0,
        headerHeight_kc: 0,
        bottomHeight_kc: 0,
        pageList_kc: [],
        pageHeight_kc: [{
          top: 0,
          height: 0,
          bottom: 0,
        }],
        loadComplete_kc: false,
        classification_load:true
      }, () => {
        let current_array = this.makeCurrentArray(id,this.data.kc_all_show?this.all_materials_kc_all:this.all_materials_kc);
        let pageList_kc = [{
              data: current_array.slice(0, 20),
              visible: true,
            }];
        this.setData({
          pageList_kc,
          classification_load:false
        });
        setTimeout(() => {
          this.initPageHeightKc(0)
        }, 100);
 
      })
      this.scroll_top.kc.right = 0;
    }else if(tabIndex==2){
      if(id==this.data.currentCategoryId_xh){
        return
      }
      page_index_xh=1;
      this.setData({
        currentCategoryId_xh: id,
        right_top_xh: 0,
        headerHeight_xh: 0,
        bottomHeight_xh: 0,
        pageList_xh: [],
        pageHeight_xh: [{
              top: 0,
              height: 0,
              bottom: 0,
            }],
        loadComplete_xh: false,
        classification_load:true
      }, () => {
        let current_array = this.makeCurrentArray(id,this.data.xh_all_show?this.all_materials_xh_all:this.all_materials_xh);
        let pageList_xh = [{
          data: current_array.slice(0, 20),
          visible: true,
        }];
        this.setData({
          pageList_xh,
          classification_load:false
        });
        setTimeout(() => {
          this.initPageHeightXh(0)
        }, 100);
      })
      this.scroll_top.xh.right = 0;
    }else if(tabIndex==3){
      if(id==this.data.currentCategoryId_sh){
        return
      }
      page_index_sh=1;
      this.setData({
        currentCategoryId_sh: id,
        right_top_sh: 0,
        headerHeight_sh: 0,
        bottomHeight_sh: 0,
        pageList_sh: [],
        pageHeight_sh: [{
          top: 0,
          height: 0,
          bottom: 0,
        }],
        loadComplete_sh: false,
        classification_load:true
      }, () => {
        let current_array = this.makeCurrentArray(id,this.data.sh_all_show?this.all_materials_sh_all:this.all_materials_sh);
        let pageList_sh = [{
          data: current_array.slice(0, 20),
          visible: true,
        }];
        this.setData({
          pageList_sh,
          classification_load:false
        });
        setTimeout(() => {
          this.initPageHeightSh(0)
        }, 100);
      })
      this.scroll_top.sh.right = 0;
    }else if(tabIndex==4){
      if(id==this.data.currentCategoryId_ck){
        return
      }
      page_index_ck=1;
      this.setData({
        currentCategoryId_ck: id,
        right_top_ck: 0,
        headerHeight_ck: 0,
        bottomHeight_ck: 0,
        pageList_ck: [],
        pageHeight_ck: [{
              top: 0,
              height: 0,
              bottom: 0,
            }],
        loadComplete_ck: false,
        classification_load:true
      }, () => {
        let current_array = this.makeCurrentArray(id,this.data.ck_all_show?this.all_materials_ck_all:this.all_materials_ck);
        let pageList_ck = [{
          data: current_array.slice(0, 20),
          visible: true,
        }];
        this.setData({
          pageList_ck,
          classification_load:false
        });
        setTimeout(() => {
          this.initPageHeightCk(0)
        }, 100);
      })
      this.scroll_top.ck.right = 0;
    }
   
  },
  searFun(){
    let titArr=["","库存","消耗","入库","出库"];
    let {tabIndex,
      // all_materials_kc_all,
      // all_materials_xh_all,
      // all_materials_sh_all,
      // all_materials_ck_all,
      choose_repository,
      date_kc,
      date_xh_start,
      date_xh_end,
      date_sh_start,
      date_sh_end,
      date_ck_start,
      date_ck_end,
      typeArr_kc,
      typeIndex_kc,
      range_options_xh
    } = this.data;
    let {all_materials_kc_all,all_materials_xh_all,all_materials_sh_all,all_materials_ck_all} = this
    let startTime="",endTime="";
    
    let arr=[];
    if(tabIndex==1){
      arr=JSON.parse(JSON.stringify(all_materials_kc_all)) ;
      startTime=date_kc.replace(/\//g,"-");
      endTime=date_kc.replace(/\//g,"-");
    }else if(tabIndex==2){
      arr=JSON.parse(JSON.stringify(all_materials_xh_all));
      startTime=date_xh_start;
      endTime=date_xh_end;  
    }else if(tabIndex==3){
      arr=JSON.parse(JSON.stringify(all_materials_sh_all));
      startTime=date_sh_start;
      endTime=date_sh_end;
    }else if(tabIndex==4){
      arr=JSON.parse(JSON.stringify(all_materials_ck_all));
      startTime=date_ck_start;
      endTime=date_ck_end;
    }
    app.data.temp.statisticsAllMaterials=arr;
    app.data.temp.range_options_xh = range_options_xh
    wx.navigateTo({
      url: '/pages/erp-statistics-items-search/erp-statistics-items-search?title='+titArr[tabIndex]+'&repository_id=' + choose_repository.id+
      "&startTime="+startTime+"&endTime="+endTime+'&type='+tabIndex+'&isNowDay='+(typeArr_kc.length == 3?'true':'false')+"&typeIndex_kc="+typeIndex_kc
    })
  },
  time: {
    now: '',
    now_1: '',
    now_7: '',
    now_30: '',
  },
  changeFilterDate_kc(e:any) {
    let {
      filter_date_choose_kc,
      filter_dates,
      typeIndex_kc,
      loadTabsCon,
    } = this.data;
    let value = e.detail.value;
    let k1 = this.data.date_kc,
    k2 = this.data.filter_date_word_kc,
    k3 = this.data.filter_date_name_kc
    // k4 = this.data.typeArr_kc
    let obj1 = JSON.stringify({k1,filter_date_choose_kc,filter_dates,typeIndex_kc,loadTabsCon,k3,k2})
    this.data_snapshot = JSON.parse(obj1)
    let date_kc="";
    if (value == 4) {
      this.setData({
        showDateRange: true,
        filter_date_choose_kc: 4
      })
      return
    }
    if (value == filter_date_choose_kc) {
      return;
    }
    let typeArr_kc=["所有状态","缺货状态"];;
    let filter_date_word_kc="";
    if (value == 1) {//昨天
      filter_date_word_kc="昨日";
      let day = new Date();
      date_kc=util.formatDate( new Date(day.getTime()-24*60*60*1000));
      
    } else if (value == 2) {
      let day = new Date();
      let dayN= day.getDay();//一周第几天，0是上周日
      if(dayN==0){
        dayN=7;
      }
      date_kc=util.getBeforeDay(nowDay,dayN);

    } else if (value == 3) {

      date_kc=util.getPrevMonth().endDate;

    }else{
      filter_date_word_kc="今日";
      date_kc=nowDay2;
      typeArr_kc=["所有状态","缺货状态","在途状态"];
    }
    if(typeArr_kc.length==2 && typeIndex_kc==2){
      typeIndex_kc=0;
    }
    let filter_date_name_kc=this.getFilterDateNameKc(date_kc);
    date_kc=date_kc.replace(/-/g,"/")
    if(date_kc==this.data.date_kc){
      return
    }
    loadTabsCon.kc=true;
    let confer = (data:any)=>{
     if (data) {
       let data_snapshot = this.data_snapshot
       this.setData({
         typeArr_kc:data_snapshot.k4,
         date_kc:data_snapshot.k1,
         typeIndex_kc:data_snapshot.typeIndex_kc,
         filter_date_choose_kc:data_snapshot.filter_date_choose_kc,
         filter_date_name_kc:data_snapshot.k3,
         filter_date_word_kc:data_snapshot.k2
       })
    }else{
      this.setData({
        typeArr_kc,
        typeIndex_kc,
        // filter_date_choose_kc: value,
        filter_date_name_kc,
        filter_date_word_kc
      })
    }
   }
    this.setData({
      date_kc,
      // typeArr_kc,
      // typeIndex_kc,
      filter_date_choose_kc: value,
      // filter_date_name_kc,
      // filter_date_word_kc
    }, () => {
      this.getKcList(confer);
    });
  },
  confirmDateRange_kc(e:any) {
    const {
      start_date
    } = e.detail;
    let {
      typeIndex_kc,loadTabsCon
    }=this.data;
    let start_arr = start_date.split('/');
    let start_date_str = start_arr[0] +"/"+ util.formatNumber(start_arr[1]) +"/"+ util.formatNumber(start_arr[2]);
    let typeArr_kc=["所有状态","缺货状态"];
    let filter_date_word_kc="";
    let day = new Date();
    let yesterday=util.formatDate(new Date(day.getTime()-24*60*60*1000)).replace(/-/g,"/");
    if(start_date_str==nowDay2){
      filter_date_word_kc="今日";
      typeArr_kc=["所有状态","缺货状态","在途状态"];
      this.setData({
        filter_date_choose_kc:0
      })
      
    }else if(start_date_str==yesterday){
      filter_date_word_kc="昨日";
      this.setData({
        filter_date_choose_kc:1
      })
    }else{
      
    }
    let filter_date_name_kc=this.getFilterDateNameKc(start_date_str);
    if(typeArr_kc.length==2 && typeIndex_kc==2){
      typeIndex_kc=0;
    }
    loadTabsCon.kc=true;
    let confer = (data:any)=>{
      if (data) {
        let data_snapshot = this.data_snapshot
        this.setData({
          typeArr_kc:data_snapshot.k4,
          date_kc:data_snapshot.k1,
          typeIndex_kc:data_snapshot.typeIndex_kc,
          filter_date_choose_kc:data_snapshot.filter_date_choose_kc,
          filter_date_name_kc:data_snapshot.k3,
          filter_date_word_kc:data_snapshot.k2
        })
     }else{
       this.setData({
         typeArr_kc,
         typeIndex_kc,
         // filter_date_choose_kc: value,
         filter_date_name_kc,
         filter_date_word_kc
       })
     }
    }
    this.setData({
      date_kc: start_date_str,
      // typeArr_kc,
      // typeIndex_kc,
      // filter_date_name_kc,
      // filter_date_word_kc,
      loadTabsCon
    }, () => {
     this.getKcList(confer);
    });
  },
  changeFilterDate_xh(e:any) {
    let {
      filter_date_choose_xh,
      // filter_dates_xh,
      tabIndex,
      filter_date_choose_sh,
      filter_date_choose_ck,
      loadTabsCon
    } = this.data;
    let value = e.detail.value;
    if (value == 6) {
      if(tabIndex==2){
        this.setData({
          showDateRange: true,
          filter_date_choose_xh: 6
        })
      }else if(tabIndex==3){
        this.setData({
          showDateRange: true,
          filter_date_choose_sh: 6
        })
      }else if(tabIndex==4){
        this.setData({
          showDateRange: true,
          filter_date_choose_ck: 6
        })
      }
      
      return
    }
    if ((value == filter_date_choose_xh && tabIndex==2) || (value == filter_date_choose_sh && tabIndex==3)|| (value == filter_date_choose_ck && tabIndex==4)) {
      return;
    }
    let start_date = null;
    let end_date = null;
    let _name = '';
    let show_name = '';
    if (value == 0) {
      start_date = nowDay;
      end_date = nowDay;
      _name = '今日';
      show_name = this.getFilterDateNameKc(nowDay);
    } else if (value == 1) {
      let day = new Date();
      let zt=util.formatDate( new Date(day.getTime()-24*60*60*1000));
      start_date = zt;
      end_date = zt;
      _name = '昨日';
      show_name = this.getFilterDateNameKc(zt);
    } else if (value == 2) {
      start_date = util.getWeekTime(0);
      end_date = util.getWeekTime(-6);
      show_name = util.getFilterDateName(start_date,end_date);
    } else if (value == 3) {
      start_date = util.getWeekTime(7);
      end_date = util.getWeekTime(1);
      show_name = util.getFilterDateName(start_date,end_date);
    }else if (value == 4){
      let nowMonth=util.currentMonthLast();
      start_date=nowMonth.startDate;
      end_date=nowDay;
      show_name = util.getFilterDateName(start_date,end_date);
    }else if (value == 5){
      let prevMonth=util.getPrevMonth();
      start_date=prevMonth.startDate;
      end_date=prevMonth.endDate;
      show_name = util.getFilterDateName(start_date,end_date);
    }
    if(tabIndex==2){
      loadTabsCon.xh=true;
      this.setData({
        filter_date_word_xh:_name,
        filter_date_name_xh: show_name,
        filter_date_choose_xh: value,
        date_xh_start: start_date,
        date_xh_end: end_date,
        loadTabsCon
      }, () => {
        this.getXhList()
      });
    }else if(tabIndex==3){
      loadTabsCon.sh=true;
      this.setData({
        filter_date_word_sh: _name,
        filter_date_name_sh: show_name,
        filter_date_choose_sh: value,
        date_sh_start: start_date,
        date_sh_end: end_date,
        loadTabsCon
      }, () => {
        this.getShList()
      });
    }else if(tabIndex==4){
      loadTabsCon.ck=true;
      this.setData({
        filter_date_word_ck: _name,
        filter_date_name_ck: show_name,
        filter_date_choose_ck: value,
        date_ck_start: start_date,
        date_ck_end: end_date,
        loadTabsCon
      }, () => {
        this.getCkList()
      });
    }
    
  },
  confirmDateRange_xh(e:any) {
    let {
      tabIndex,loadTabsCon
    }=this.data;
    const {
      start_date,
      end_date,
    } = e.detail;
    
    let start_arr = start_date.split('/');
    let end_arr = end_date.split('/');
    let now_arr:any = nowDay.split('-');//今天
    let now_str=now_arr[0]+"/"+(now_arr[1]*1)+"/"+(now_arr[2]*1);
    let now1_arr:any = now_1.split('-');//昨天
    let now1_str=now1_arr[0]+"/"+(now1_arr[1]*1)+"/"+(now1_arr[2]*1);
    let filter_date_name = start_date.substr(2) + ' - ' + end_date.substr(2);
    let start_date_str = start_arr[0] + '-' +util.formatNumber(start_arr[1])+ '-' +util.formatNumber(start_arr[2]);
    let end_date_str = end_arr[0]+ '-' +util.formatNumber(end_arr[1])+ '-' +util.formatNumber(end_arr[2]);
    let filter_date_word = '';
    if(start_date==now_str && end_date==now_str ){
      filter_date_word="今日";
      filter_date_name = this.getFilterDateNameKc(nowDay);
      if(tabIndex==2){
        this.setData({
          filter_date_choose_xh:0
        })
      }else if(tabIndex==3){
        this.setData({
          filter_date_choose_sh:0
        })
      }else if(tabIndex==4){
        this.setData({
          filter_date_choose_ck:0
        })
      }
    }else if(start_date==now1_str && end_date==now1_str ){
      filter_date_word="昨日";
      filter_date_name = this.getFilterDateNameKc(now_1);
      if(tabIndex==2){
        this.setData({
          filter_date_choose_xh:1
        })
      }else if(tabIndex==3){
        this.setData({
          filter_date_choose_sh:1
        })
      }else if(tabIndex==4){
        this.setData({
          filter_date_choose_ck:1
        })
      }
    }else if (start_arr[0] == now_arr[0] && end_arr[0] == now_arr[0]) {
      filter_date_name = start_arr[1] + '/' + start_arr[2] + ' - ' + end_arr[1] + '/' + end_arr[2]
    }
    
    if(tabIndex==2){
      loadTabsCon.xh=true;
      this.setData({
        filter_date_word_xh: filter_date_word,
        filter_date_name_xh: filter_date_name,
        date_xh_start: start_date_str,
        date_xh_end: end_date_str,
        loadTabsCon
      }, () => {
        this.getXhList()
      });
    }else if(tabIndex==3){
      loadTabsCon.sh=true;
      this.setData({
        filter_date_word_sh: filter_date_word,
        filter_date_name_sh: filter_date_name,
        date_sh_start: start_date_str,
        date_sh_end: end_date_str,
        loadTabsCon
      }, () => {
        this.getShList()
      });
    }else if(tabIndex==4){
      loadTabsCon.ck=true;
      this.setData({
        filter_date_word_ck: filter_date_word,
        filter_date_name_ck: filter_date_name,
        date_ck_start: start_date_str,
        date_ck_end: end_date_str,
        loadTabsCon
      }, () => {
        this.getCkList()
      });
    }
    
  },
  
  getFilterDateNameKc(day:any){
    let date=new Date();
    let year = date.getFullYear();
    let starY=day.substring(2,4);
    let starM=(day.substring(5,7))*1;
    let starD=(day.substring(8,10))*1;
    if(year==day.substring(0,4)){
      return starM+"/"+starD
    }else{
      return starY+"/"+starM+"/"+starD
    }
  },
 //返回上一级
 returnPreviousLevel(){
   wx.switchTab({
    url:"../function-application/function-application"
  })
 },
 //刷新当前选项卡
 refreshCurrentOptions(){
   let amtion = this.data.amtion
   let index = this.data.tabIndex
   if (!amtion) {
    this.setData({
      amtion:true
    },()=>{
     if (index==1) {
        this.getKcList();
     }else if (index==2) {  
        this.getXhList();
     }else if (index==3) {
        this.getShList();
     }else if (index==4) {
        this.getCkList();
     }
    })
   }
 },
 //筛选框展开
 screen(e:any){
  let offon = e.currentTarget.dataset.button
  let {range_options,screen_button} = this.data
  if (screen_button) {
    range_options = JSON.parse(JSON.stringify(this.last_range_options))
  }
  this.setData({
    screen_button:!offon,
    range_options,
  })
 },
 //选择筛选选项
 changeQuick(e:any){
  const value = e.currentTarget.dataset.name
  const type = e.currentTarget.dataset.type
  const type1 = e.currentTarget.dataset.type1
  let range_options = this.data.range_options
  if (type!=='ylfw') {
    if (!value&&range_options[type]&&!range_options[type1]) {
      wx.showToast({
        title:'您需要至少选择一个选项哦',
        icon:'none',
        duration:2000
      }) 
      return
    }
  }
  range_options[type] = value
  this.setData({
    range_options
  })
  
 },
 //确认筛选
 determineScreeningResults(e:any){
  const value = e.currentTarget.dataset.index
  const obj = this.data.range_options
  const obj1 = this.range_options
  let screen_button_bule = this.data.screen_button_bule
  if (obj.cbdw===obj1.cbdw&&obj.kcdw===obj1.kcdw&&obj.ylfw===obj1.ylfw) {
    this.setData({
      screen_button:false
    })
    return
  }
  if (obj.ylfw=='hcyl'&&obj.kcdw==true&&obj.cbdw==false) {
    screen_button_bule = false
  }else{
    screen_button_bule = true
  }
  if (value==='kc') {
    this.radioKc()
  }
  if (value==='xh') {
    this.radioXh()
  }
  if (value==='sh') {
    this.radioSh()
  }
  if (value==='ck') {
    this.radioCk()
  }
  this.last_range_options = JSON.parse(JSON.stringify(this.data.range_options))
  this.setData({
    screen_button:false,
    screen_button_bule
  })
 },
 toResetFilter(){
  this.setData({
    range_options:{
      ylfw:'hcyl',
      kcdw: true,
      cbdw:false
    }
  })
 }
});
export{}